package org.yxj.requestlibaray.bean;

import android.util.Log;
import com.google.gson.Gson;

import org.yxj.requestlib.util.MD5Util;
import org.yxj.requestlib.bean.PostData;

import java.util.HashMap;
import java.util.Map;
import okhttp3.RequestBody;

public class BaseConfig {

    private  String UserId = "aaaa";        //  账号ID
    private  int Device = 3;            //  1ios手机、2ios Pad、3安卓手机、4安卓Pad、5web手机、6web电脑、11WEB钻石批发商城、12WEB商城前端（非钻石）
    private  long Time = 0 ;            //  当前时间戳
    private  String VerId = "";         //  当前版本Id
    private  String OnlineMark = "111";    //  上线标识类似Token
    private  String Sign = "";          //  sign=MD5(UserId|Device|Time|VerId|Key20190517|OnlineMark)
    private  String key="YlQ6iR#ox4BEqP4vchhI3_YHWFNFysQU";
    private HashMap<String,Object> postMap;

    private BaseConfig(){
    }

    private BaseConfig(Builder config){
        postMap = config.map ;
    }

    public String getOnlineMark() {
        return OnlineMark;
    }


    public Map<String,Object> map(){
        postMap.put("UserId",UserId);
        postMap.put("Device",Device);
        postMap.put("VerId",VerId);
        postMap.put("Time",Time);
        postMap.put("OnlineMark",OnlineMark);
        postMap.put("Sign", MD5Util.getMD5(getBaseMD5Sign().toString()));
        Map<String , Object> jsonContent=new HashMap<>();
        jsonContent.put("data",postMap);
        Log.v("[ApiRequest]","onNext - > o = "+new Gson().toJson(postMap));
        return jsonContent;
    }

    public RequestBody body(){
        postMap.put("UserId",UserId);
        postMap.put("Device",Device);
        postMap.put("VerId",VerId);
        postMap.put("Time",Time);
        postMap.put("OnlineMark",OnlineMark);
        postMap.put("Sign", MD5Util.getMD5(getBaseMD5Sign().toString()));
        Map<String , Object> jsonContent=new HashMap<>();
        jsonContent.put("data",postMap);
        Log.v("[ApiRequest]","onNext - > o = "+new Gson().toJson(postMap));
        return PostData.postStrContentRequestBody(jsonContent);
    }


    /**
     * 默认  加密的数据
     *
     * @return
     */
    protected StringBuilder getBaseMD5Sign(){
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(UserId+"|"+Device+"|"+Time+"|"+VerId+"|"+ key+"|"+ OnlineMark);
        return stringBuilder;
    }



    public static class Builder {

        private HashMap<String,Object> map = new HashMap<>();
        /**
         * @param otherMap
         * @return
         */
        public Builder setPostMap(HashMap<String,Object> otherMap){
            map.putAll(otherMap);
            return this;
        }

        /**
         * @return
         */
        public BaseConfig build(){
            return new BaseConfig(this) ;
        }
    }
}
